﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

namespace OF.JsonRpc.Client
{
    /// <summary>
    /// 
    /// </summary>
    public class CallAsyncResult<T> : IAsyncResult
    {
        /// <summary>
        /// 
        /// </summary>
        public CallAsyncResult()
        {
            isCompleted = false;
        }

        internal bool IsDeclaredParams { get; set; }
        internal object DeclaredParam { get; set; }

        public T Result { get; set; }
        public Exception Error { get; set; }
        internal RpcOption RpcOption { get; set; }
        internal object[] Args { get; set; }
        internal object DeclaredArgs { get; set; }

        internal Action<T> SuccessedCallBack { get; set; }
        internal Action<Exception> FailedCallBack { get; set; }

        private Object asyncState;
        public object AsyncState
        {
            get
            {
                return asyncState;
            }
        }

        public bool CompletedSynchronously
        {
            get
            {
                return false;
            }
        }

        public WaitHandle AsyncWaitHandle
        {
            get
            {
                // not supported
                return null;
            }
        }

        private Boolean isCompleted;
        public bool IsCompleted
        {
            get
            {
                return isCompleted;
            }
        }

        public string Method { get; set; }

        internal void SetCompleted()
        {
            isCompleted = true;
            if (SuccessedCallBack != null)
            {
                if (this.Error == null)
                    SuccessedCallBack(this.Result);
            }
            if (FailedCallBack != null)
            {
                if (this.Error != null)
                    FailedCallBack(this.Error);
            }
        }
    }
}
